<template>
  <div :class="{ disabled: fileList.length > 0 }">
    <el-upload
      list-type="picture-card"
      :limit="1"
      action="#"
      :file-list="fileList"
      :on-preview="handLePictureCardPreviw"
      :on-remove="handleRomove"
      :on-change="handleChange"
      :before-upload="handleBeforeUpload"
      :http-request="upload"
    >
      <i class="el-icon-plus"></i>
    </el-upload>
    <!-- 使用进度条 -->
    <el-progress
      v-if="showPercent"
      :percentage="percentPage"
      style="width: 180px"
    ></el-progress>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
  </div>
</template>

<script>
var COS = require('cos-js-sdk-v5')
var cos = new COS({
  SecretId: 'AKIDIPHuJTO4UfaHiuqXyA6nEQM2TrzsWv5z',
  SecretKey: 'h8JEtJTO4VkjTxCBuNbZIlyvd5IFwOjp'
})
export default {
  name: 'ImageUpload',
  components: {},
  props: {},
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      fileList: [],
      // 定义进度条数据
      showPercent: false,
      percentPage: 0,
      uploading: false // 上传文件中
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    handleRomove(file, fileList) {
      this.fileList = this.fileList.filter(t => t !== file)
    },
    handleChange(file, fileList) {
      this.fileList.push(file)
    },
    handLePictureCardPreviw(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleBeforeUpload({ type, size }) {
      // if (['image/jpeg', 'image/gif', 'image/bmp', 'image/png'].find(t => t === type)) {
      // if (['image/jpeg', 'image/gif', 'image/bmp', 'image/png'].indexOf(type) !== -1) {
      // if (['image/jpeg', 'image/gif', 'image/bmp', 'image/png'].findIndex(t => t === type) !== -1) {
      if (!['image/jpeg', 'image/gif', 'image/bmp', 'image/png'].find(t => t === type)) {
        this.$message.error('图片类型不对')
        return false
      }
      if (size > 5 * 1024 * 1024) {
        this.$message.error('图片大小不对')
        return false
      }
      //   校验成功,显示进度条
      this.showPercent = true
      return true
    },
    upload({ file }) {
      this.uploading = true
      cos.putObject({
        Bucket: 'rzzt-1310733630', // 存储桶名称
        Region: 'ap-chengdu', // 地域
        Key: Date.now() + file.name, // 文件名
        StorageClass: 'ATANDARD', // 上传的模式类型 直接默认 标准模式即可
        Body: file,
        onProgress: function(progressData) {
          // 上传显示进度条
        //   console.log(JSON.stringify(progressData))
          this.percentPage = progressData.percent * 100
        }
      }, (err, data) => {
        // console.log(err || data)
        if (!err && data.statusCode === 200) {
          this.fileList[0].url = `http://${data.Location}`
          //   上传完成隐藏进度条
          this.showPercent = false
          this.uploading = false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
// .disabled .el-upload--picture-card[data-v-82a94682]
// .disabled .el-upload--picture-card{
//[data-v-82a94682] .disabled .el-upload--picture-card
::v-deep.disabled .el-upload--picture-card {
  display: none !important;
}
</style>>
